package com.ccys.cms.controller.productAndService;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.MedicalServiceListDetailService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.web.productAndService.BatchDetailServiceDto;
import com.ccys.db.dto.web.productAndService.MedicalServiceListDetailDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblMedicalServiceListDetail;
import com.ccys.db.mapper.TblMedicalServiceListDetailMapper;
import com.ccys.db.vo.web.productAndService.ExistServiceVo;
import com.ccys.db.vo.web.productAndService.MedicalServiceListDetailPackageVo;
import com.ccys.db.vo.web.productAndService.MedicalServiceListDetailVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "医疗服务集合详情")
public class MedicalServiceListDetailController {


    @Resource
    private MedicalServiceListDetailService service;

    @Resource
    private TblMedicalServiceListDetailMapper serviceListDetailMapper;

    @ApiOperation("批量添加Service / Package")
    @PostMapping("medicalServiceListDetail")
    @Transactional
    public ResponseResult saveBatch(@RequestBody List<BatchDetailServiceDto> dtoList) {
        if (CollectionUtils.isNotEmpty(dtoList)) {
            BatchDetailServiceDto dto = dtoList.get(0);
            String listId = dto.getListId();
            Integer serviceType = dto.getServiceType();

            if (0 == serviceType) {
                //查询service是否已经存在
                List<ExistServiceVo> existServiceList = serviceListDetailMapper.getExistServiceList(listId, dtoList);
                if (CollectionUtils.isNotEmpty(existServiceList)) {
                    return ResponseResult.error("Service " + existServiceList.get(0).getName() + " already exists");
                }

            } else if (1 == serviceType) {
                //查询package是否已经存在
                List<ExistServiceVo> existPackageList = serviceListDetailMapper.getExistPackageList(listId, dtoList);
                if (CollectionUtils.isNotEmpty(existPackageList)) {
                    return ResponseResult.error("Package " + existPackageList.get(0).getName() + " already exists");
                }
            } else {
                return ResponseResult.error("please input correct serviceType");
            }
            List<TblMedicalServiceListDetail> collect = dtoList.stream().map(x -> {
                TblMedicalServiceListDetail detail = new TblMedicalServiceListDetail();
                detail.setId(IdUtil.getSnowflakeNextIdStr());
                detail.setListId(listId);
                //0:medical service
                //1:medical service package
                detail.setServiceType(serviceType);
                detail.setMedicalServiceId(x.getMedicalServiceId());
                detail.setCreatedBy(UserUtil.getCurLoginUserId());
                detail.setCreatedOnUtc(new Date().getTime());
                return detail;
            }).collect(Collectors.toList());
            int i = service.insertAll(collect);

            return ResponseResult.update(i);

        }
        return ResponseResult.success();

    }

    @DeleteMapping("medicalServiceListDetail")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblMedicalServiceListDetail model = new TblMedicalServiceListDetail();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        return ResponseResult.update(i);
    }

    @GetMapping("getServicePage")
    @ApiOperation(value = "查询service分页")
    public ResponseResult<PageInfo<MedicalServiceListDetailVo>, Object> getServicePage(MedicalServiceListDetailDto dto) {
        //0:medical service
        //1:medical service package
        return service.getServicePage(dto);
    }

    @GetMapping("getPackagePage")
    @ApiOperation(value = "查询package分页")
    public ResponseResult<PageInfo<MedicalServiceListDetailPackageVo>, Object> getPackagePage(MedicalServiceListDetailDto dto) {
        //0:medical service
        //1:medical service package
        return service.getPackagePage(dto);
    }

    @GetMapping("getServiceListByPackageId")
    @ApiOperation("查询package下的service集合")
    public ResponseResult<MedicalServiceListDetailVo, Object> getServiceListByPackageId(String packageId) {

        return service.getServiceListByPackageId(packageId);
    }
}
